package com.gxa.p2p.controller;


import com.alibaba.fastjson.JSON;
import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.config.JavaEmailSender;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.UserInfo;
import com.gxa.p2p.service.UserInfoService;
import com.gxa.p2p.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/UserInfo")
@Api(value = "用户个人信息管理控制层",tags = "用户个人信息管理模块")
public class UserInfoController {

    static String code;
    @Resource(name = "userInfoServiceImpl")
    UserInfoService userInfoService;
    @ResponseBody
    @GetMapping("/findInfo")
    @ApiOperation("查询用户信息接口")
    public Map<String, Object> findInfo(@RequestParam("userId") Integer id){
        // 需要优化 查询用户头像存放地址，并将绝对路径存入对象中返回给前端
        UserInfo info = userInfoService.findInfo(id);
        MapUtil mapUtil = new MapUtil();
        if (info != null){
            mapUtil.setCode(0);
            mapUtil.setMsg("查询成功");
            mapUtil.put("data",info);
        }else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    @ResponseBody
    @PostMapping("/saveInfo")
    @ApiOperation(value = "新增用户信息接口",tags = "添加用户信息接口",
            httpMethod = "Post",response = Map.class)
    public Map<String, Object> saveInfo(@RequestBody @ApiParam("用户个人信息数据") UserInfo userInfo){
        MapUtil mapUtil = new MapUtil();
        if (userInfoService.saveUserInfo(userInfo)){
            mapUtil.setCode(0);
            mapUtil.setMsg("信息添加成功！！");
        }else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    @ResponseBody
    @PutMapping("/updateInfo")
    @ApiOperation(value = "更新用户信息接口",tags = "更新用户信息接口",
            httpMethod = "Put",response = Map.class)
    public Map<String, Object> updateInfo(@RequestBody @ApiParam UserInfo userInfo){
        MapUtil mapUtil = new MapUtil();
        if (userInfoService.updateInfo(userInfo)){
            mapUtil.setCode(0);
            mapUtil.setMsg("信息修改成功");
        }else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    @ResponseBody
    @PostMapping("/ImgUpload")
    @ApiOperation(value = "头像上传接口",tags = "用户头像上传接口",
    httpMethod = "Post",response = Map.class)
    public Map<String, Object> uploadAvatar(@RequestParam("file") List<MultipartFile> multipartFile,@RequestParam("userId") Integer userId, HttpServletRequest request) throws IOException {
        List<Map<String, Object>> upload = FileUploadUtil.upload(multipartFile, request);
        System.out.println("UserId:"+userId+"    "+upload.get(0));
        UserInfo info = userInfoService.findInfo(userId);
        info.setAvatar((String) upload.get(0).get("fileName"));
        MapUtil mapUtil = new MapUtil();
        if (userInfoService.updateInfo(info)){
            mapUtil.setCode(0);
            mapUtil.setMsg("信息修改成功");
        }else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }


    @RequestMapping("email")
    @ResponseBody
    public Map <String, Object> mailSucc(@RequestBody Map<String,String> map) throws Exception {
        String toEMAIL = map.get("email");         //对方邮箱
        int mobile_code = (int) ((Math.random() * 9 + 1) * 100000); // 有一定的规范,不可随意生成随机数
        code =String.valueOf(mobile_code);
        System.out.println(code);
        String content = "您的验证码是：" + mobile_code + "。请不要把验证码泄露给其他人。";
        String CONTENT = toEMAIL + "验证码发送成功";        //内容1
        JavaEmailSender.sendEmail(toEMAIL, content, CONTENT);
        MapUtil mapUtil=new MapUtil();
        mapUtil.put("code", 0);
        mapUtil.put("msg", "邮件发送成功");
        return mapUtil.getMap();

    }

    @ResponseBody
    //这个注解表示你的返回值将存在responsebody中返回到前端，前端接收后会显示将数据到页面，页面不会跳转。
    @PostMapping("/UpdateById")
    @ApiOperation(value = "更改邮箱接口",tags = "邮箱上传接口",httpMethod = "Post",response = Map.class)
    public Map<String, Object> updateById(@RequestBody Map<String,Object> map, HttpServletRequest request) throws Exception {
        System.out.println(map.get("userid"));
        System.out.println(map.get("mapcode"));
        System.out.println(map.get("email"));
        //System.out.println(request.getParameter("email"));
        //验证邮箱验证码是否正确
        CompareCodeUtil compareCodeUtil=new CompareCodeUtil();
        compareCodeUtil.compare((String) map.get("mapcode"),code);
        //强转操作
        Integer userid = JSON.parseObject(JSON.toJSONString(map.get("userid")), Integer.class);
        String email = JSON.parseObject(JSON.toJSONString(map.get("email")), String.class);
        System.out.println(userid+" qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq "+email);
        Boolean aBoolean = userInfoService.updateById(userid,email );
        MapUtil mapUtil=new MapUtil();
        if (aBoolean){
            mapUtil.setCode(0);
            mapUtil.setMsg("邮箱绑定成功！！");
//            SendMailUtil sendMailUtil=new SendMailUtil();
//            sendMailUtil.mailSucc(request);

        }else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();

    }


}
